const sequelize = require("../app/db");
const { DataTypes, Model } = require("sequelize");

class User extends Model {
  getHidePhoneNumber() {
    return this.PhoneNumber.replace(this.PhoneNumber.substring(3, 7), "****");
  }
  getHideCardNo() {
    return this.CardNo.replace(/(?<=\d{3})\d{11}(?=\d{3})/, "************");
  }
}
User.init(
  {
    Id: {
      type: DataTypes.INTEGER,
      allowNull: false, //不允许为空
      primaryKey: true,
      autoIncrement: true,
    },
    AvatarUrl: {
      type: DataTypes.STRING,
    },
    Nickname: {
      type: DataTypes.STRING,
    },
    PhoneNumber: {
      type: DataTypes.STRING,
    },
    Password: {
      type: DataTypes.STRING,
    },
    Sex: {
      type: DataTypes.INTEGER,
    },
    Birthday: {
      type: DataTypes.STRING,
    },
    Description: {
      type: DataTypes.STRING,
    },
    IsAuthor: {
      type: DataTypes.INTEGER,
    },
    Email: {
      type: DataTypes.STRING,
    },
    TrueName: {
      type: DataTypes.STRING,
    },
    CardNo: {
      type: DataTypes.STRING,
    },
    Status: {
      type: DataTypes.INTEGER,
    },
    CreateTime: {
      type: DataTypes.STRING,
      get() {
        return new Date(this.getDataValue("CreateTime")).getTime();
      },
    },
    ModifyTime: {
      type: DataTypes.STRING,
      get() {
        return new Date(this.getDataValue("ModifyTime")).getTime();
      },
    },
  },
  {
    sequelize,
    modelName: "User",
    tableName: "user", //强制设置表名
  }
);

module.exports = User;
